package com.wenyu.wenyuyuedu.activity.personage;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.activity.LoginActivity;
import com.wenyu.wenyuyuedu.bean.personage.WithDrawCashBean;
import com.wenyu.wenyuyuedu.global.ApiWrapper;
import com.wenyu.wenyuyuedu.server.personage.WithDrawCashServer;
import com.wenyu.wenyuyuedu.utils.AbSharedUtil;
import com.wenyu.wenyuyuedu.utils.MyToast;
import com.wenyu.wenyuyuedu.utils.SharedConstants;
import com.wenyu.wenyuyuedu.utils.StatusBarUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static anetwork.channel.http.NetworkSdkSetting.context;

/*
* 体现
* */
public class EmbodyActivity extends AppCompatActivity implements View.OnClickListener {

    @InjectView(R.id.playtour_left)
    ImageView playtourLeft;
    @InjectView(R.id.et_money)
    EditText etMoney;
    @InjectView(R.id.et_wx)
    EditText etWx;
    @InjectView(R.id.et_alipay)
    EditText etAlipay;
    @InjectView(R.id.button_tixian)
    Button buttonTixian;
    private Activity activity;
    //余额
    private int yuMoney;
    private String trim;
    private AlertDialog tiXianDialog;
    private double corruntMoney1;
    private double money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_embody);
        ButterKnife.inject(this);
        this.activity = EmbodyActivity.this;
        StatusBarUtil.setStatusBar(EmbodyActivity.this, R.color.white);
        inviControl();
    }

    /*
    * 初始化控件
    * */
    private void inviControl() {
        Intent intentGet = getIntent();
        money = intentGet.getDoubleExtra("money", 0);
        buttonTixian.setOnClickListener(this);
        playtourLeft.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            /*返回*/
            case R.id.playtour_left:
                finish();
                break;
            /*体现*/
            case R.id.button_tixian:
                trim = etMoney.getText().toString().trim();
                if (TextUtils.isEmpty(trim)) {
                    MyToast.showToast(EmbodyActivity.this, "请输入金额", Toast.LENGTH_SHORT);
                } else {
                    double corruntMoney = Double.parseDouble(trim);
                    corruntMoney1 = (double) corruntMoney * 100;
                    if (corruntMoney > money) {
                        MyToast.showToast(EmbodyActivity.this, "提现金额大于余额", Toast.LENGTH_SHORT);
                    } else {
                        String wxPlay = etWx.getText().toString().trim();
                        String aliPlay = etAlipay.getText().toString().trim();
                        if (TextUtils.isEmpty(trim)) {
                            MyToast.showToast(EmbodyActivity.this, "请输入金额", Toast.LENGTH_SHORT);
                        } else if (TextUtils.isEmpty(wxPlay) && TextUtils.isEmpty(aliPlay)) {
                            MyToast.showToast(EmbodyActivity.this, "请选择提现方式", Toast.LENGTH_SHORT);
                        } else {
                            String regEx = "^[0-9]+(.[0-9]{1,2})?$";
                            Pattern pattern = Pattern.compile(regEx);
                            Matcher matcher = pattern.matcher(trim);
                            boolean b = matcher.matches();
                            // boolean mat3ches = reg.matches(trim);
                            if (b) {
                                if (TextUtils.isEmpty(wxPlay)) {
                                    //支付宝支付
                                    String TiXianTypes = aliPlay;
                                    String TiXianType = 1 + "";
                                    TixianData(TiXianType, TiXianTypes);
                                } else if (TextUtils.isEmpty(aliPlay)) {
                                    //微信支付
                                    String TiXianTypes = wxPlay;
                                    String TiXianType = 0 + "";
                                    TixianData(TiXianType, TiXianTypes);
                                } else {
                                    MyToast.showToast(EmbodyActivity.this, "只能选择一种提现方式", Toast.LENGTH_SHORT);
                                }

                            } else {
                                MyToast.showToast(EmbodyActivity.this, "输入金额不正确", Toast.LENGTH_SHORT);
                            }
                        }
                    }
                }
                // MyToast.showToast(EmbodyActivity.this, "你有钱嘛？乱点", Toast.LENGTH_SHORT);
                break;
        }
    }

    /*提现的接口*/
    public void TixianData(String TiXianType, String TiXianTypes) {
        String token = AbSharedUtil.getString(activity, SharedConstants.USER_TOKEN);
        WithDrawCashServer withDrawCashServer = ApiWrapper.getInstance().create(WithDrawCashServer.class);
        Call<WithDrawCashBean> withDrawCashBeanCall = withDrawCashServer.postWithDrawCash(token, corruntMoney1 + "", TiXianType, TiXianTypes);
        withDrawCashBeanCall.enqueue(new Callback<WithDrawCashBean>() {
            @Override
            public void onResponse(Call<WithDrawCashBean> call, Response<WithDrawCashBean> response) {
                WithDrawCashBean body = response.body();
                if (body != null) {
                    int code = body.getCode();
                    if (code == 1) {
                        TiXianSuccessDailog();
                    } else if (code == 0) {
                        showLoginDoalog();
                    } else if (code == 5900) {
                        MyToast.showToast(EmbodyActivity.this, "提现金额最少20元哦!", Toast.LENGTH_SHORT);
                    } else if (code == 5901) {
                        MyToast.showToast(EmbodyActivity.this, "提现金额最高不能超过1000元的!", Toast.LENGTH_SHORT);
                    } else if (code == 5902) {
                        MyToast.showToast(EmbodyActivity.this, "您的账户余额不够！", Toast.LENGTH_SHORT);
                    } else if (code == 5903) {
                        MyToast.showToast(EmbodyActivity.this, "提现的间隔时间需要超过24小时哦！", Toast.LENGTH_SHORT);
                    } else if (code == 5904) {
                        MyToast.showToast(EmbodyActivity.this, "系统繁忙，请稍后重试！", Toast.LENGTH_SHORT);
                    }
                }
            }

            @Override
            public void onFailure(Call<WithDrawCashBean> call, Throwable t) {
                MyToast.showToast(EmbodyActivity.this, "系统繁忙，请稍后重试！", Toast.LENGTH_SHORT);
            }
        });
    }

    /*提现成功的dailog*/
    private void TiXianSuccessDailog() {
        tiXianDialog = new AlertDialog.Builder(activity).create();
        tiXianDialog.show();
        Window window = tiXianDialog.getWindow();
        window.setContentView(R.layout.dailog_tixian);
        TextView TixanQueDing = (TextView) window.findViewById(R.id.dialog_login_quxiao);
        TixanQueDing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tiXianDialog.dismiss();
                finish();
            }
        });
    }

    android.app.AlertDialog ad;
    private TextView quxiao;
    private TextView queding;

    /*显示登陆的dialog*/
    private void showLoginDoalog() {

        ad = new android.app.AlertDialog.Builder(context).create();
        ad.show();
        Window window = ad.getWindow();
        window.setContentView(R.layout.dailog_login);
        quxiao = (TextView) window.findViewById(R.id.dialog_login_quxiao);
        queding = (TextView) window.findViewById(R.id.dialog_login_queding);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(activity, LoginActivity.class);
                startActivity(intent);
                finish();
                ad.dismiss();
            }
        });
    }
}
